//数据模块
var sqlHelper=require('../conf/sqlHelper');

//读取问题对应答案数据列表信息
function getAnswerByQuestion(question,callback) {

	// body...
	var sqlstr="select *  from automsg WHERE question like ?";
	var param=['%'+question+'%'];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results.length>0){   

            //结果大于一条随机选取
            var num=Math.round(Math.random()*(results.length-1));
            var result=results[parseInt(num)];
            

            //console.log(Math.random(1));
            //console.log(parseInt(num));
            //console.log('随机：'+result.answer);
            
    		callback(result.answer);
    	}else{
            callback('我不懂你在说什么')
        }    		
    });
}

//添加问题库
function insertAnswerWithQuestion(question,answer,callback){

    var sqlstr='insert into automsg(question,answer)values(?,?)';
    var param=[question,answer];


    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}



exports.getAnswerByQuestion=getAnswerByQuestion;
exports.insertAnswerWithQuestion=insertAnswerWithQuestion;